package org.mikhaylov.cp.web.command;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Main interface for the Command pattern implementation.
 * 
 * @author Saint
 */
public abstract class WebCommand implements Serializable {

	protected String forward;
	
	private static final long serialVersionUID = 6203032379692957403L;
	
	protected abstract boolean checkParams(HttpServletRequest request);
	
	protected abstract boolean extractParams(HttpServletRequest request);
	
	protected abstract void process(HttpServletRequest request);
	
	/**
	 * Execution method for command.
	 * 
	 * @return Address to go once the command is executed.
	 */
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		if (checkParams(request)) {
			if (extractParams(request)) {
				process(request);
			}
		}

		return forward;
	}

	@Override
	public final String toString() {
		return getClass().getSimpleName();
	}
}